/*
 * 编辑任务测试
 * */
Ext.define('MobileTest.view.window.EditTaskActionWindow', {
			extend:'Ext.window.Window',
			width:800,
			height:450,
			modal:true,
 			title:'新建下发任务',				
			ids:null,
			rec:null,						
			initComponent : function() {
				
			var me = this;
			
			var kpistore=Ext.create('Ext.data.Store', {    	
    			fields:[
 					'name','content','createUser',
    				{name:'createDt',type:'date'},{name: 'id',type:'int'}
    			],    	
    			proxy: {
        			type: 'ajax',
        			url : 'getKpiGroups',
        			reader: {
            			type: 'json',
            			root: 'rows'
        			}        	
    			}
			});
	
				var devicestore = Ext.create('Ext.data.Store', {
							fields:[
 								'text',{name: 'id',type:'int'}
    						],
							proxy: {
            					type: 'ajax',
            					url: 'DeviceTree'            					
        					}
						});
			
			
			var editfrm= Ext.create('Ext.form.Panel', {    			
    					bodyPadding: 5,    				
    					region:'center',
    					layout: 'anchor',
    					defaults: {
        					anchor: '95%',        					
    					},
    					 fieldDefaults: {
            				labelAlign: 'right'            				
        				},
    					defaultType: 'textfield',
    					items: [{
        									fieldLabel: '任务名称',
        									name: 'TestTaskName',
        									anchor:'50%',
        									allowBlank: false
    									},{
        									fieldLabel: '描述',
        									name: 'TestTaskDesc',
        									allowBlank: false
    									},{
        		                    		xtype : 'fieldcontainer',
        		                    		defaultType: 'textfield',
        		                    		anchor:'100%',
        		                    		layout:'hbox',
        		            			 	fieldDefaults: {
            									labelAlign: 'right'            				
        									},
        		                    		items:[{
        	        		                    fieldLabel: '日志级别',
        	        		                    allowBlank: false,
        	        		                    xtype:'combobox',
    									  		store: Ext.create('MobileTest.store.LogLevel'),
    									  		queryMode: 'local',
    									  		displayField: 'name',
    									  		valueField: 'value',
        	        		                    name: 'LogLevel'
        	        		                },{
        		                    xtype      : 'fieldcontainer',
        		                    fieldLabel : '抓包',
        		                    defaultType: 'radiofield',
        		                    defaults: {
        		                        flex: 1
        		                    },
        		                    layout: 'hbox',
        		                    items: [
        		                        {
        		                            boxLabel  : '需要',
        		                            name      : 'NetCapture',        		                            
        		                            inputValue: 1,
        		                            id        : 'NetCapture_radio1',
        		                            listeners:{"change":function( t,  newValue,  oldValue, eOpts){
        		                            	        		                            	
        		                            	
        		                            	
        		                            	
        		                            }}
        		                        }, {
        		                            boxLabel  : '不需要',
        		                            name      : 'NetCapture',
        		                            checked :   true,
        		                            inputValue: 0,
        		                            id        : 'NetCapture_radio2'
        		                        }
        		                    ]
        		                } ]
        		                    	},{
        		                    		xtype : 'fieldcontainer',
        		                    		defaultType: 'textfield',
        		                    		anchor:'100%',
        		                    		layout:'hbox',
        		                    		fieldDefaults: {
            									labelAlign: 'right'            				
        									},
        		                    		items:[{
        	        		                    fieldLabel: '类型',
        	        		                    allowBlank: false,
        	        		                    xtype:'combobox',
    											store: Ext.create('MobileTest.store.TestTaskType'),
    											queryMode: 'local',
    											displayField: 'name',
    											valueField: 'value',
        	        		                    name: 'TestTaskType'
        	        		                }, {
        	        		                    fieldLabel: '优先级',
        	        		                    allowBlank: false,
        	        		                    xtype:'combobox',
    											store: Ext.create('MobileTest.store.Priority'),
    											queryMode: 'local',
    											displayField: 'name',
    											valueField: 'value',
        	        		                    name: 'Priority'
        	        		                }]        		                    		
        		                    	},{
    				fieldLabel : '开始时间',
    				xtype : 'fieldcontainer',    			
    				layout:'hbox',
    				items:[{
    					xtype : 'datefield',
    					name : 'txt_begin_date',
    					format : 'Y-m-d',    					
    					value:new Date() ,
    					allowBlank: false 
    				},{
    					xtype : 'timefield',
    					name : 'txt_begin_time',
    					format : 'H:i',
    					value:'00:00',    				
    					allowBlank: false
    				}]							
    			},{
    				fieldLabel : '结束时间',
    				xtype : 'fieldcontainer',    			
    				layout:'hbox',
    				items:[{
    					xtype : 'datefield',
    					name : 'txt_end_date',
    					format : 'Y-m-d',    				
    					value:new Date() ,
    					allowBlank: false 
    				},{
    					xtype : 'timefield',
    					name : 'txt_end_time',
    					format : 'H:i',    				
    					value:'23:59',
    					allowBlank: false
    				}]							
    			},{    		
    				xtype : 'fieldcontainer',    			
    				layout:'hbox',
    				anchor:'100%',
    				fieldDefaults: {
            				labelAlign: 'right'            				
        			},
    				items:[
    					{
        		                		  fieldLabel: '业务组',
        		                		  allowBlank: false,           		                		 
        		                		  store:kpistore, 
        		                		  xtype:'combobox',
    									  queryMode: 'local',
    									  displayField: 'name',
    									  valueField: 'id',   		                		
        		                		  name: 'business_ddl'
        		                	  },{
        		                		  fieldLabel: '终端组',
        		                		  allowBlank: false,        		  
        		                		  store:devicestore, 
        		                		  xtype:'combobox',
    									  queryMode: 'local',
    									  displayField: 'text',
    									  valueField: 'id', 
        		                		  name: 'mobile_ddl'
        		                	  }
    				]},	{
        		                xtype:'fieldset',
        		                title: '回捞清单生成规则配置',         		             
        		                columnWidth: 0.5,
        		                collapsible: true,
        		                defaultType: 'textfield',
        		                defaults: {anchor: '90%'},    
        		                collapsed: false,         		              
        		                layout:'anchor',
        		                items :[
        		                  {
        		                	  xtype : 'fieldcontainer',
        		                	  defaultType: 'textfield',
        		                	  anchor:'100%',
        		                	  fieldDefaults: {
            							labelAlign: 'right'            				
        					    	  },
        		                	  layout:'hbox',
        		                	  items:[{
        		                    xtype      : 'fieldcontainer',
        		                    fieldLabel : '执行类型',
        		                    defaultType: 'radiofield',
        		                    defaults: {
        		                        flex: 1
        		                    },
        		                    layout: 'hbox',
        		                    anchor:'100%',
        		                    items: [
        		                        {
        		                            boxLabel  : '按时执行',
        		                            name      : 'ExeType',        		                            
        		                            inputValue: 1,
        		                            id        : 'ExeType_radio1',
        		                            listeners:{"change":function( t,  newValue,  oldValue, eOpts){
        		                            	        		   
        		                            	
        		                            	
        		                            }}
        		                        }, {
        		                            boxLabel  : '按次执行',
        		                            name      : 'ExeType',
        		                            checked :   true,
        		                            inputValue: 2,
        		                            id        : 'ExeType_radio2'
        		                        }
        		                    ]
        		                },{
        		                		  fieldLabel: '重复类型',
        		                		  allowBlank: false,
        		                		  xtype:'combobox',
    									  store: Ext.create('MobileTest.store.IterationType'),
    									  queryMode: 'local',
    									  displayField: 'name',
    									  valueField: 'value',
        		                		  name: 'IterationType'
        		                	  }]
        		                  },
        		                  {
        		                	  xtype : 'fieldcontainer',
        		                	  defaultType: 'textfield',
        		                	  anchor:'100%',
        		                	  layout:'hbox',
        		                	  fieldDefaults: {
            							labelAlign: 'right'            				
        					    	  },
        		                	  items:[{
        		                		  fieldLabel: '开始时间段',
        		                		  allowBlank: false,
        		                		  xtype: 'timefield', 
        		                		  format:'H:i:s'    ,   		                		  
        		                		  name: 'ExeBeginTime'
        		                	  },{
        		                		  fieldLabel: '结束时间段',
        		                		  allowBlank: false,
        		                		  xtype: 'timefield',        		                		  
        		                		  format:'H:i:s'    ,
        		                		  name: 'ExeEndTime'
        		                	  }]
        		                  },
        		                  {
        		                	  xtype : 'fieldcontainer',
        		                	  defaultType: 'textfield',
        		                	  anchor:'100%',
        		                	  fieldDefaults: {
            							labelAlign: 'right'            				
        					    	  },
        		                	  layout:'hbox',
        		                	  items:[{
        		                		  fieldLabel: '循环次数',
        		                		  allowBlank: false,
        		                		  xtype: 'numberfield',
        		                		  minValue: 0,
        		                		  value:0,
        		                		  name: 'IterationNum'
        		                	  },{
        		                		  fieldLabel: '间隔时间',
        		                		  allowBlank: false,
        		                		  xtype: 'numberfield',
        		                		  minValue: 0,
        		                		  value:0,
        		                		  name: '_Interval'
        		                	  }]
        		                  }
        		                ]},{
        							xtype: 'hiddenfield',
        							name: 'TestTaskCode',
        							value: ''
    							}        		                    	
        		                        ]}
    				);
			
		Ext.applyIf(me, {
			layout:'border',  
			items:[	editfrm],
			dockedItems:[ {
					xtype : 'toolbar',
					dock : 'top',
					items : [ {
							text : '保存',
							tooltip : '保存添加测试任务',
							iconCls : 'add',
							handler : function() {
								
								var form = me.down('form').getForm();
            					if (form.isValid()) {
            						
            						var temp= form.findField("txt_begin_date").value;
									var begin_date=Ext.Date.format(temp,'Ymd');
		
									temp=form.findField("txt_begin_time").getValue();
									var begin_time=Ext.Date.format(temp,'Hi');
		
									temp=form.findField("txt_end_date").getValue();		
									var end_date=Ext.Date.format(temp,'Ymd');
		
									temp=form.findField("txt_end_time").getValue();
									var end_time=Ext.Date.format(temp,'Hi');
            						
                					form.submit({
                						url: 'submitTask',
    									params: {        									
        									StartDate:begin_date+begin_time,
        									EndDate:end_date+end_time        									        								
    									},
                    					success: function(form, action) {
                       						Ext.Msg.alert('Success', action.result.msg);
                       						me.close();
                       						
                    					},
                    					failure: function(form, action) {
                        					Ext.Msg.alert('Failed', action.result.msg);
                    					}
                					});
            					}
            					
							}
						}, {
							text : '关闭',
							tooltip : '关闭窗口',
							iconCls : 'add',
							handler : function() {
									me.close();
							}
					}]
			}]
		});
				me.callParent(arguments);	
				
				me.on('beforerender',me.on_beforerender);		
				
				devicestore.load({params:{node:0}});	
				kpistore.load({params:{key:''}});
					
	},
	on_beforerender:function(me, eOpts){
		
		var rec=me.rec;
				
		if(rec!=null){
			
			var form=me.child('form').getForm();
			
			//form.loadRecord(rec);
			
			form.findField('TestTaskCode').setValue(rec.data.testTaskCode);
			//form.findField('TestTaskFlag').setValue(rec.data.testTaskFlag);			
			form.findField('TestTaskName').setValue(rec.data.testTaskName);
			form.findField('TestTaskDesc').setValue(rec.data.testTaskDesc);
			form.findField('LogLevel').setValue(rec.data.logLevel);
			
			form.findField('NetCapture').setValue(rec.data.netCapture);
			form.findField('TestTaskType').setValue(rec.data.testTaskType);
			form.findField('Priority').setValue(rec.data.priority);
				
			var full_startDate_str=rec.data.startDate;
			var full_endDate_str=rec.data.endDate;
			
			var startDate=full_startDate_str.substring(0,8);
			var startTime=full_startDate_str.substring(8);
			
			var endDate=full_endDate_str.substring(0,8);
			var endTime=full_endDate_str.substring(8);
			
			form.findField('txt_begin_date').setValue(startDate.insert(6,'-').insert(4,'-'));
			form.findField('txt_begin_time').setValue(startTime.insert(2,':'));
			
			form.findField('txt_end_date').setValue(endDate.insert(6,'-').insert(4,'-'));
			form.findField('txt_end_time').setValue(endTime.insert(2,':'));
			
			form.findField('ExeType').setValue(rec.data.exeType);
			form.findField('ExeBeginTime').setValue(rec.data.exeBeginTime.insert(4,':').insert(2,':'));
			form.findField('ExeEndTime').setValue(rec.data.exeEndTime.insert(4,':').insert(2,':'));
			form.findField('IterationType').setValue(rec.data.iterationType);
			form.findField('IterationNum').setValue(rec.data.iterationNum);
			
			form.findField('_Interval').setValue(rec.data._Interval);
		
			form.findField('business_ddl').setValue(rec.data.businessGroup);
			form.findField('mobile_ddl').setValue(rec.data.deviceGroup);				
		}
		
	}
					
		});
 